package edu.strategy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

import edu.commander.Commander;
import edu.random.Randonizador;

public class DataManager {
	String urlForSave;
	protected DataManager(String urlForSave){
		this.urlForSave = urlForSave;
	}
	public boolean save(Map<String, Randonizador> map){
		try{
			FileOutputStream fo = new FileOutputStream(urlForSave + ".map");
			ObjectOutputStream oo = new ObjectOutputStream(fo);
			oo.writeObject(map);
			oo.close();
		} catch (Exception e){
			e.printStackTrace();
		}
		return true;
	}
	public Map<String, Randonizador> getSavedMap() throws Exception{
		FileInputStream fi = new FileInputStream(urlForSave + ".map");
		ObjectInputStream oi = new ObjectInputStream(fi);
		Map<String, Randonizador> map =(Map) oi.readObject();
		oi.close();
		return map;
	}
	public boolean save(Commander commander){
		try{
			FileOutputStream fo = new FileOutputStream(urlForSave + ".com");
			ObjectOutputStream oo = new ObjectOutputStream(fo);
			oo.writeObject(commander);
			oo.close();
		} catch (Exception e){
			e.printStackTrace();
		}
		return true;
	}
	public Commander getSavedCommander() throws Exception {
		FileInputStream fi = new FileInputStream(urlForSave + ".com");
		ObjectInputStream oi = new ObjectInputStream(fi);
		Commander commander =(Commander) oi.readObject();
		oi.close();
		return commander;
	}
}
